﻿namespace TestTool.Tests.Common.TestEngine
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;

    public class RTSPSimulator
    {
        private string _hostIP;
        private int _httpPort = 0x1f40;
        private int _rtspPort = 0x216a;
        private Dictionary<Codecs, Dictionary<string, string>> _rtspUrls = new Dictionary<Codecs, Dictionary<string, string>>();
        private IAsyncResult _runResult;
        private Dictionary<Codecs, Dictionary<string, string>> _streams = new Dictionary<Codecs, Dictionary<string, string>>();
        private int _timeout;
        private Action<int> _waitSeverStarted;

        public RTSPSimulator(string hostIP, Action<int> waitSeverStarted, int timeout)
        {
            this._waitSeverStarted = waitSeverStarted;
            this._timeout = timeout;
            this._hostIP = hostIP;
        }

        public void Add(Codecs codec, string file, string name)
        {
            if (!this._streams.ContainsKey(codec))
            {
                this._streams[codec] = new Dictionary<string, string>();
            }
            this._streams[codec][name] = file;
        }

        [DllImport("RTSPSimulator.dll")]
        private static extern void AddSampleFile(int codec, string file, string name);
        [DllImport("RTSPSimulator.dll")]
        private static extern string GetRtspUrl(int codec, string name);
        public string GetUrl(Codecs codec, string name)
        {
            if (this._rtspUrls.ContainsKey(codec) && this._rtspUrls[codec].ContainsKey(name))
            {
                return this._rtspUrls[codec][name];
            }
            return string.Empty;
        }

        public string[] GetUrls(Codecs codec)
        {
            List<string> list = new List<string>();
            if (this._rtspUrls.ContainsKey(codec))
            {
                foreach (string str in this._rtspUrls[codec].Values)
                {
                    list.Add(str);
                }
            }
            return list.ToArray();
        }

        [DllImport("RTSPSimulator.dll")]
        private static extern int IsRtspOverHttp();
        [DllImport("RTSPSimulator.dll")]
        private static extern int IsStarted();
        [DllImport("RTSPSimulator.dll")]
        private static extern void Start(uint codecs, int rtspPort, int httpPort);
        public bool StartRTSP()
        {
            Action action2 = null;
            this.StopRTSP();
            this._rtspUrls.Clear();
            int useCodecs = 0;
            foreach (Codecs codecs in this._streams.Keys)
            {
                useCodecs |= 1 << (int)codecs;
                foreach (string str in this._streams[codecs].Keys)
                {
                    AddSampleFile((int)codecs, this._streams[codecs][str], str);
                }
            }
            if (useCodecs == 0)
            {
                return false;
            }
            if (action2 == null)
            {
                action2 = delegate
                {
                    Start((uint)useCodecs, this._rtspPort, this._httpPort);
                };
            }
            this._runResult = action2.BeginInvoke(null, null);
            while (IsStarted() != 1)
            {
                this._waitSeverStarted(10);
            }
            foreach (Codecs codecs2 in this._streams.Keys)
            {
                foreach (string str2 in this._streams[codecs2].Keys)
                {
                    string rtspUrl = GetRtspUrl((int)codecs2, str2);
                    if (!string.IsNullOrEmpty(rtspUrl))
                    {
                        string oldValue = rtspUrl.Substring(rtspUrl.IndexOf("://") + 3);
                        oldValue = oldValue.Substring(0, oldValue.IndexOfAny(new char[] { ':', '/' }));
                        rtspUrl = rtspUrl.Replace(oldValue, this._hostIP);
                    }
                    if (!this._rtspUrls.ContainsKey(codecs2))
                    {
                        this._rtspUrls[codecs2] = new Dictionary<string, string>();
                    }
                    this._rtspUrls[codecs2][str2] = rtspUrl;
                }
            }
            return true;
        }

        [DllImport("RTSPSimulator.dll")]
        private static extern void Stop();
        public void StopRTSP()
        {
            Stop();
            if (this._runResult != null)
            {
                if (!this._runResult.IsCompleted)
                {
                    this._runResult.AsyncWaitHandle.WaitOne(this._timeout);
                }
                this._runResult.AsyncWaitHandle.Close();
            }
        }

        public enum Codecs
        {
            JPEG,
            MPEG4,
            H264,
            G711,
            G726,
            AAC
        }
    }
}

